﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BioAttendance.Data;

namespace BioAttendance
{
    public partial class MainPageAttendance : Form
    {
        private BioAttendanceEntities DbEntities;
        private List<EmployeeStatus> EmpStatus = new List<EmployeeStatus>();

        public MainPageAttendance()
        {
            InitializeComponent();
            this.DbEntities = new BioAttendanceEntities();
            this.FormClosed += new FormClosedEventHandler(MainPageAttendance_FormClosed);
            UpdateData();
            UpdateDate();
            this.employeeStatusBindingSource.DataSource = this.EmpStatus;
            timer.Start();
        }

        private void MainPageAttendance_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.DbEntities.Dispose();
        }

        private EmployeeStatus GetEmpStatus(Attendance att)
        {
            var item = (from empstat in this.EmpStatus
                        where empstat.Belongs(att)
                         orderby empstat.EntryTime ascending
                         select empstat).Take(1);
            if (item.Count() > 0)
            {
                return item.ElementAt(0);
            }
            return new EmployeeStatus(att);
        }

        private EmployeeStatus GetEmpStatus(Employee emp)
        {
            var item = (from empstat in this.EmpStatus
                        where empstat.Belongs(emp)
                        orderby empstat.EntryTime ascending
                        select empstat).Take(1);
            if (item.Count() > 0)
            {
                return item.ElementAt(0);
            }
            return null;
        }

        private void buttonEmpEntry_Click(object sender, EventArgs e)
        {
            using (VerificationForm verificationForm = new VerificationForm(this.DbEntities))
            {
                verificationForm.ShowDialog();
                if (verificationForm.Verified)
                {
                    EmployeeStatus empStat = GetEmpStatus(verificationForm.VerifiedEmployee);
                    if (empStat != null)
                    {
                        MessageBox.Show("Employee {0} already signed in. Exit first and then make an Entry.");
                    }
                    else
                    {
                        Attendance attendance = new Attendance();
                        attendance.Employee = verificationForm.VerifiedEmployee;
                        attendance.EntryTime = verificationForm.AttendanceTime.Value;
                        attendance.EntryNote = verificationForm.Note;
                        this.EmpStatus.Add(new EmployeeStatus(attendance));
                        this.DbEntities.AddToAttendances(attendance);
                        this.DbEntities.SaveChanges();
                        UpdateView();
                    }
                }
            }
        }

        private void buttonEmpExit_Click(object sender, EventArgs e)
        {
            using (VerificationForm verificationForm = new VerificationForm(this.DbEntities))
            {
                verificationForm.ShowDialog();
                if (verificationForm.Verified)
                {
                    EmployeeStatus empStat = GetEmpStatus(verificationForm.VerifiedEmployee);
                    if (empStat == null)
                    {
                        MessageBox.Show("Employee {0} not signed in.", verificationForm.VerifiedEmployee.Name);
                    }
                    else
                    {
                        Attendance attendance = empStat.Attendance;
                        attendance.IsClosed = true;
                        attendance.ExitTime = verificationForm.AttendanceTime.Value;
                        attendance.ExitNote = verificationForm.Note;
                        this.EmpStatus.Remove(empStat);
                        this.DbEntities.SaveChanges();
                        UpdateView();
                    }
                }
            }
        }

        private void UpdateView()
        {
            this.employeeStatusBindingSource.DataSource = null;
            this.employeeStatusBindingSource.DataSource = this.EmpStatus;
        }

        private void UpdateData()
        {
            this.EmpStatus = new List<EmployeeStatus>();
            var items = from att in this.DbEntities.Attendances
                        where !att.IsClosed
                        select att;
            foreach (var item in items)
            {
                this.EmpStatus.Add(GetEmpStatus(item));
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            UpdateDate();
        }

        private void UpdateDate()
        {
            if (this.InvokeRequired)
            {
                Invoke(new MethodInvoker(UpdateDate));
            }
            else
            {
                DateTime dt = DateTime.Now;
                this.labelTime.Text = dt.ToString(@"d MMMM h:m:s tt");
                for (int i = 0; i < this.dataGridViewEmpIN.RowCount; i++)
                {
                    this.dataGridViewEmpIN.UpdateCellValue(3,i);
                }
            }
        }
        
    }
}
